package com.my.blogapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.my.blogapi.dao.mapper.ArticleMapper;
import com.my.blogapi.dao.pojo.Article;
import com.my.blogapi.service.ThreadService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

@Service
public class ThreadServiceImpl implements ThreadService {


    //此操作期望在多线程的环境下执行
    @Async("taskExecutor") //指定线程池名称
    @Override
    public void updateArticleViewCount(ArticleMapper articleMapper, Article article) {
        Article articleUpdate = new Article();
        articleUpdate.setViewCounts(article.getViewCounts()+1);
        LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Article::getId,article.getId());
        updateWrapper.eq(Article::getViewCounts,article.getViewCounts());
        articleMapper.update(articleUpdate,updateWrapper);
        try {
            //睡眠5秒 证明不会影响主线程的使用
            Thread.sleep(5000);
            System.out.println("阅读数更新完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
